package humanDetector;


import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;


import pdi.IImg2ImgOperation;
import pdi.IO.ImgReader;
import pdi.IO.ImgWriter;
import pdi.core.Image;
import pdi.preprocessing.AbstractPreProcessing;
import pdi.segmentation.AbstractSegmentation;


import classifiers.AbstractClassifier;
import classifiers.SVMLinear;

import de.bwaldvogel.liblinear.Parameter;
import de.bwaldvogel.liblinear.SolverType;
import featureExtractors.AbstractFeatureExtraction;

import tests.LibsIntegrationTest;

public class BasicHumanDetector extends AbstractHumanDetector {
		
	private AbstractPreProcessing preprocessor;
	private AbstractSegmentation segmentator;
	private AbstractFeatureExtraction featureExtactor;
	private AbstractClassifier classifier;
	
	

	public BasicHumanDetector(AbstractPreProcessing preprocessor,
			AbstractSegmentation segmentator,
			AbstractFeatureExtraction featureExtactor, AbstractClassifier classifier) {
		super();
		this.preprocessor = preprocessor;
		this.segmentator = segmentator;
		this.featureExtactor = featureExtactor;
		this.classifier = classifier;
	}

	@Override
	public void train(List<Image> images) throws Exception {
		if(images != null && images.size() > 0 && 
				(images.get(0).getFeatureVector() == null || images.get(0).getFeatureVector().length == 0)){
			throw new IllegalArgumentException("Image's features need to be precomputed");
		}
		
		//classifier.train(images);
	}
	
	@Override
	public void genFeaturesFile(ImgReader reader, ImgWriter writer){
		
		reader.open();
		writer.open();

		Image img = null;
		int count = 0;
		while((img = reader.readNext()) != null){
			
			List<Image> segImgs = segmentator.execute(preprocessor.execute(img));
			
			for (Image window : segImgs) {
				count++;
				window = featureExtactor.execute(window);
				writer.writeNext(window);

				if((count % 1000) == 0)
					System.out.println(count + " images processed");
			}				

		}

		reader.close();
		writer.close();				
	}

	@Override
	public List<Image> detect(Image img) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
